package e_stream;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

class MyEditor extends JFrame implements ActionListener
{
	String fileName="noname";
	boolean modifyFlag = false; // 편집상태값 false=수정안됨, true=수정됨
	final String title ="MyEditor : ";
	
	JMenuBar bar = new JMenuBar();
	JMenu fileMenu = new JMenu("파일");

	JMenuItem newItem = new JMenuItem("새글");
	JMenuItem openItem = new JMenuItem("열기");
	JMenuItem saveItem = new JMenuItem("저장");
	JMenuItem saveAsItem = new JMenuItem("다른 이름으로 저장");
	JMenuItem exitItem = new JMenuItem("종료");
	
	Container cont;
	JTextArea textArea;
	JScrollPane scroll;
	
	JToolBar tbar = new JToolBar();
	JButton newTool = new JButton();
	JButton openTool = new JButton();
	JButton saveTool = new JButton();
	

	public MyEditor()
	{
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				exitProcess();
			}
		});
		setMenu();
		initForm();
		
		this.setBounds(100,100,600,400);
		this.setVisible(true);
	}
	
	public void setMenu()
	{
		this.setJMenuBar(bar);
		bar.add(fileMenu);
		
		fileMenu.add(newItem);
		fileMenu.add(openItem);
		fileMenu.add(saveItem);
		fileMenu.add(saveAsItem);
		fileMenu.add(exitItem);
		
		newItem.addActionListener(this);
		openItem.addActionListener(this);
		saveItem.addActionListener(this);
		saveAsItem.addActionListener(this);
		exitItem.addActionListener(this);
	}
	
	public void initForm()
	{
		this.setTitle(title + fileName);
		cont = this.getContentPane();
		textArea = new JTextArea();

		textArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent key) {
				System.out.println(key.getKeyCode());
				if(key.getKeyCode() == 32 || key.getKeyCode() >40 ){ 
					modifyFlag=true;
					setTitle(title + fileName + "(*)");
				}
			}
		});
		scroll = new JScrollPane(textArea);
		cont.add(scroll);
		
		
		newTool.setIcon(new ImageIcon("newfile.png"));
		openTool.setIcon(new ImageIcon("openfile.png"));
		saveTool.setIcon(new ImageIcon("savefile.png"));
		cont.add(tbar,BorderLayout.SOUTH);
		
		tbar.add(newTool);
		tbar.add(openTool);
		tbar.add(saveTool);

		newTool.addActionListener(this);
		openTool.addActionListener(this);
		saveTool.addActionListener(this);
		
	}

	
	public void actionPerformed(ActionEvent ev)
	{
		if(ev.getSource() == newItem   || ev.getSource() == newTool){
			fileNewProcess();
		}else if(ev.getSource() == openItem  || ev.getSource() == openTool){
			fileOpenProcess();
		}else if(ev.getSource() == saveItem  || ev.getSource() == saveTool){
			fileSaveProcess();
		}else if(ev.getSource() == saveAsItem){
			 if( fileSaveAsProcess()) saveFile();
		}else if(ev.getSource() == exitItem){
			exitProcess();
		}
	}
	
	// 기존 파일이 편집되어 있으면 저장 여부 판단 후 초기화
	public void fileNewProcess()
	{
		// 기존파일 편집 여부 체크
		if(modifyFlag){
			int yn = JOptionPane.showConfirmDialog(this, 
										"파일이 수정되었습니다. 저장하쇼 !!!",
										"수정된 파일 저장",
										JOptionPane.YES_NO_OPTION
										);
			if(yn == JOptionPane.OK_OPTION) fileSaveProcess();
		}

		fileName = "noname";
		textArea.setText("");
		this.setTitle(title + fileName);
		modifyFlag = false;
		
	}
	
	// 기존 파일의 저장 여부 체크
	// 
	public void fileOpenProcess()
	{
		// 기존파일 편집 여부 체크
		if(modifyFlag){
			int yn = JOptionPane.showConfirmDialog(this, 
										"파일이 수정되었습니다. 저장하쇼 !!!",
										"수정된 파일 저장",
										JOptionPane.YES_NO_OPTION
										);
			if(yn == JOptionPane.OK_OPTION){
				fileSaveProcess();
			}
		}
		fileOpen();
		
	}
	
	public void fileOpen(){
		FileReader fr =null;
		BufferedReader buffer = null;
		String str = null;
		JFileChooser fc = new JFileChooser();

		//필터 정의
		//fc.addChoosableFileFilter(new MyFilter("txt","Text File"));
		//fc.addChoosableFileFilter(new MyFilter("*","*"));
		
		fc.addChoosableFileFilter(new MyFilter("html","HTML File"));
		fc.addChoosableFileFilter(new MyFilter("java","Java File"));

		fc.setFileFilter(new MyFilter("txt","Text File"));
		int yn = fc.showOpenDialog(this);
		if(yn != JFileChooser.APPROVE_OPTION) return;
		
		fileName = fc.getSelectedFile().toString();
		
		this.setTitle(title + fileName);
			
		textArea.setText("");

		try{
			fr = new FileReader(fileName);
			buffer = new BufferedReader(fr);
			
			while( (str = buffer.readLine() ) != null){
				textArea.append(str + "\n");
			}
		
		}catch(Exception ex){
			
		}finally{
			try{
				buffer.close();
				fr.close();
			}catch(Exception ex2){}
		}
		
	}
	
	public void fileSaveProcess()
	{

		if(fileName.equals("noname")){
			if( !fileSaveAsProcess() ) return;
			saveFile();

		}else{
			saveFile();
		}

	}
	
	public void saveFile()
	{		
	
		FileWriter fw = null;
		BufferedWriter buffer = null;

		try	{
			fw = new FileWriter(fileName);
			buffer = new BufferedWriter(fw);

			String str = textArea.getText();
			buffer.write(str,0,str.length());			

			setTitle(title + fileName);
			modifyFlag = false;


		}catch(Exception ex){
			
		}finally{
			try{
				buffer.close();
				fw.close();
			}catch (Exception e) {}
		}
	}
	
	public boolean fileSaveAsProcess()
	{
		JFileChooser fc = new JFileChooser();
		int yn = fc.showSaveDialog(this);
		
		if(yn != JFileChooser.APPROVE_OPTION){
			 fileName = "noname";
			 return false;
		}
		
		fileName = fc.getSelectedFile().toString();
		
		this.setTitle(title + fileName);
		
		return true;
	
	}
		
	public void exitProcess(){
		if(modifyFlag){
			int yn = JOptionPane.showConfirmDialog(this, 
					"파일이 수정되었습니다. 저장하쇼 !!!",
					"수정된 파일 저장",
					JOptionPane.YES_NO_OPTION
					);
			if(yn == JOptionPane.OK_OPTION){
				fileSaveProcess();
			}

		}
		
		System.exit(0);
		
	}
	public static void main(String args[])
	{
		MyEditor me = new MyEditor();
	}
	
}




